﻿using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;
using DAO;
using BUS;
public partial class Admin_Pages_WRLANGUAGEManager : BasePage
{
    public static bool Insert;
    BUS_WRLANGUAGE busWRLANGUAGE = new BUS_WRLANGUAGE();
    private void Show(string Mess)
    {
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('" + Mess + "')", true);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Page.MaintainScrollPositionOnPostBack = true;
            pnUpdate.Visible = false;
            pnShow.Visible = true;
            BindGrid();
        }
    }

    private void BindGrid()
    {
        grvWRLANGUAGE.DataSource = busWRLANGUAGE.Get_Language_List();
        //grvWRLANGUAGE.DataSource = busWRLANGUAGE._GetList().Select(p=>new
        //{
        //    p.ID,
        //    p.LangName,
        //    p.LangCode,
        //    p.LangDescription,
        //    p.Active,
        //    p.Keywords,
        //    CreateDate = Convert.ToDateTime(p.CreateDate).ToShortDateString(),
        //    UpdateDate = Convert.ToDateTime(p.UpdateDate).ToShortDateString()
        //}).ToList();
        grvWRLANGUAGE.DataBind();
    }

    public void BindDrop()
    {
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        pnUpdate.Visible = true;
        pnShow.Visible = false;
        Insert = true;
        txtLangName.Text = "";
        txtLangCode.Text = "";
        txtLangDescription.Text = "";
        txtKeywords.Text = "";


    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (grvWRLANGUAGE.Items.Count > 0)
        {
            for (int i = 0; i < grvWRLANGUAGE.Items.Count; i++)
            {
                CheckBox chkSelect = (CheckBox)grvWRLANGUAGE.Items[i].FindControl("chkSelect");
                if (chkSelect.Checked)
                {
                    string Id = grvWRLANGUAGE.Items[i].Cells[1].Text;
                    try
                    {
                        busWRLANGUAGE._Delete(Convert.ToInt32(Id));
                    }
                    catch
                    {
                    }
                }
            }
        }
        BindGrid();
    }

    protected void btnOk_Click(object sender, EventArgs e)
    {
        if (txtLangName.Text.Trim().Equals(""))
        {
            Show("Bạn chưa nhập LangName!");
            txtLangName.Focus();
            return;
        }
        if (txtLangCode.Text.Trim().Equals(""))
        {
            Show("Bạn chưa nhập LangCode!");
            txtLangCode.Focus();
            return;
        }
        if (txtLangDescription.Text.Trim().Equals(""))
        {
            Show("Bạn chưa nhập LangDescription!");
            txtLangDescription.Focus();
            return;
        }
        if (txtKeywords.Text.Trim().Equals(""))
        {
            Show("Bạn chưa nhập Keywords!");
            txtKeywords.Focus();
            return;
        }


        WRLANGUAGE obj = new WRLANGUAGE();
        
        obj.LangName = txtLangName.Text;
        obj.LangCode = txtLangCode.Text;
        obj.LangDescription = txtLangDescription.Text;
        obj.Active = dropActive.SelectedValue;
        obj.Keywords = txtKeywords.Text;
        obj.UpdateDate = DateTime.Now;
        obj.IDUpdateBy = Convert.ToInt32(Session["userid"].ToString());

        if (Insert)
        {
            obj.CreateDate = DateTime.Now;
            busWRLANGUAGE._Add(obj);
        }
        else
        {
            obj.ID = Convert.ToInt32(txtID.Text);
            busWRLANGUAGE._Update(obj);
        }
        BindGrid();
        pnUpdate.Visible = false;
        pnShow.Visible = true;
        Insert = false;

    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        pnUpdate.Visible = false;
        pnShow.Visible = true;

    }

    protected void grvWRLANGUAGE_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        string Id = e.CommandArgument.ToString();
        if (e.CommandName == "Edit")
        {
            Insert = false;
            WRLANGUAGE _WRLANGUAGE = busWRLANGUAGE._GetObject(Convert.ToInt32(Id));
            txtID.Text = Id;
            txtLangName.Text = _WRLANGUAGE.LangName;
            txtLangCode.Text = _WRLANGUAGE.LangCode;
            txtLangDescription.Text = _WRLANGUAGE.LangDescription;
            dropActive.SelectedValue = _WRLANGUAGE.Active.ToString();
            txtKeywords.Text = _WRLANGUAGE.Keywords;

            pnUpdate.Visible = true;
            pnShow.Visible = false;
        }
        else if (e.CommandName == "Delete")
        {
            busWRLANGUAGE._Delete(Convert.ToInt32(Id));
            BindGrid();
        }

    }

    protected void grvWRLANGUAGE_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        grvWRLANGUAGE.CurrentPageIndex = e.NewPageIndex;
        BindGrid();
    }
}
